﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class PCheckWin : DevExpress.XtraEditors.XtraForm
    {
        DBHelper db;
        Dictionary<string, string> pars;
        public PCheckWin(Dictionary<string,string> dic)
        {
            InitializeComponent();
            db = new DBHelper();
            pars = dic;
        }

        private void PCheckWin_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "生产检验";
                //tbProCode.Text = pars["proCode"];
                //tbProName.Text = pars["proName"];
                //tbSpec.Text = pars["spec"];
                //tbProName.ReadOnly = true;
                //tbProCode.ReadOnly = true;
                //tbSpec.ReadOnly = true;
                loadJanYanFangshi();
                loadJanYanLeiXing();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tbBarCode_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    if (string.IsNullOrWhiteSpace(tbBarCode.Text))
                    {
                        MessageBox.Show("请录入条码", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    checkBarCode();
                }
          
          
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
       /// <summary>
       /// 检验类型数据
       /// </summary>
        private void loadJanYanLeiXing()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select code,name from sys_dictionaries_info where status=1 ");
            sb.Append(" and types='jianyan' ");
            var table = db.getData(sb.ToString(), "");
            cbJYLX.DataSource = table;
            cbJYLX.ValueMember = "code";
            cbJYLX.DisplayMember = "name";
        }
        /// <summary>
        /// 检验方式
        /// </summary>
        private void loadJanYanFangshi()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select code,name from sys_dictionaries_info where status=1 ");
            sb.Append(" and types='zixunjian' ");
            var table = db.getData(sb.ToString(), "");
            cbJYFS.DataSource = table;
            cbJYFS.ValueMember = "code";
            cbJYFS.DisplayMember = "name";
        }
        private void checkBarCode()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from V_BarCode_Info where ");
            sb.Append("barCode='" + tbBarCode.Text + "'");
            var table = db.getData(sb.ToString(),"");
            if (table.Rows.Count > 0)
            {
                tbProCode.Text = table.Rows[0]["proCode"].ToString();
                tbProCode.ReadOnly = true;
                tbProName.Text = table.Rows[0]["proName"].ToString();
                tbProName.ReadOnly = true;
                tbProName.Text = table.Rows[0]["proName"].ToString();
                tbProName.ReadOnly = true;
                tbSpec.Text = table.Rows[0]["spec"].ToString();
                tbSpec.ReadOnly = true;
                btnCheck.Enabled = true;
            }
            else
            {
                MessageBox.Show("条码错误", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                btnCheck.Enabled = false;
            }
        }

        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnCheck_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("insert into pi_ProductCheck_info(barCode,cType");
                sb.Append(",dType,cthickness,mocad,mocaj,touguang,createtime,");
                sb.Append("createuser,result) values('"+ tbBarCode.Text +"',");
                sb.Append("'"+ cbJYLX.SelectedValue +"','"+ cbJYFS.SelectedValue +"',");
                sb.Append("'"+ tbHouDu.Text +"','"+ tbMoCaD.Text +"','"+ tbMoCaJ.Text +"',");
                sb.Append("'" + tbTouGuang.Text + "','" + DateTime.Now.ToString() + "','" + CommonMethods.userCode + "',");
                sb.Append("'')");
                var jg = db.UpdateData(sb.ToString());
                if (int.Parse(jg) > 0)
                {
                    MessageBox.Show("数据保存成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("数据保存失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
